<!DOCTYPE HTML>
<html lang="zh-Hans" class="sidebar-visible no-js light">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>TypeScript 4.6 - TypeScript 使用指南手册</title>


        <!-- Custom HTML head -->
        
        <meta name="description" content="TypeScript Handbook 中文翻译。">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="icon" href="../../favicon.svg">
        <link rel="shortcut icon" href="../../favicon.png">
        <link rel="stylesheet" href="../../css/variables.css">
        <link rel="stylesheet" href="../../css/general.css">
        <link rel="stylesheet" href="../../css/chrome.css">
        <link rel="stylesheet" href="../../css/print.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="../../fonts/fonts.css">

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="../../highlight.css">
        <link rel="stylesheet" href="../../tomorrow-night.css">
        <link rel="stylesheet" href="../../ayu-highlight.css">

        <!-- Custom theme stylesheets -->

    </head>
    <body>
    <div id="body-container">
        <!-- Provide site root to javascript -->
        <script>
            var path_to_root = "../../";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script>
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script>
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script>
            var html = document.querySelector('html');
            var sidebar = null;
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            } else {
                sidebar = 'hidden';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded affix "><a href="../../PREFACE.html">前言</a></li><li class="chapter-item expanded affix "><li class="part-title">快速上手</li><li class="chapter-item expanded "><a href="../../zh/tutorials/index.html"><strong aria-hidden="true">1.</strong> 快速上手</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../zh/tutorials/typescript-in-5-minutes.html"><strong aria-hidden="true">1.1.</strong> 5 分钟了解 TypeScript</a></li><li class="chapter-item expanded "><a href="../../zh/tutorials/asp.net-core.html"><strong aria-hidden="true">1.2.</strong> ASP.NET Core</a></li><li class="chapter-item expanded "><a href="../../zh/tutorials/asp.net-4.html"><strong aria-hidden="true">1.3.</strong> ASP.NET 4</a></li><li class="chapter-item expanded "><a href="../../zh/tutorials/gulp.html"><strong aria-hidden="true">1.4.</strong> Gulp</a></li><li class="chapter-item expanded "><a href="../../zh/tutorials/knockout.html"><strong aria-hidden="true">1.5.</strong> Knockout.js</a></li><li class="chapter-item expanded "><a href="../../zh/tutorials/react-and-webpack.html"><strong aria-hidden="true">1.6.</strong> React 与 webpack</a></li><li class="chapter-item expanded "><a href="../../zh/tutorials/react.html"><strong aria-hidden="true">1.7.</strong> React</a></li><li class="chapter-item expanded "><a href="../../zh/tutorials/angular-2.html"><strong aria-hidden="true">1.8.</strong> Angular 2</a></li><li class="chapter-item expanded "><a href="../../zh/tutorials/migrating-from-javascript.html"><strong aria-hidden="true">1.9.</strong> 从 JavaScript 迁移到 TypeScript</a></li></ol></li><li class="chapter-item expanded "><li class="part-title">手册</li><li class="chapter-item expanded "><a href="../../zh/handbook/index.html"><strong aria-hidden="true">2.</strong> 手册</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../zh/handbook/basic-types.html"><strong aria-hidden="true">2.1.</strong> 基础类型</a></li><li class="chapter-item expanded "><a href="../../zh/handbook/interfaces.html"><strong aria-hidden="true">2.2.</strong> 接口</a></li><li class="chapter-item expanded "><a href="../../zh/handbook/functions.html"><strong aria-hidden="true">2.3.</strong> 函数</a></li><li class="chapter-item expanded "><a href="../../zh/handbook/literal-types.html"><strong aria-hidden="true">2.4.</strong> 字面量类型</a></li><li class="chapter-item expanded "><a href="../../zh/handbook/unions-and-intersections.html"><strong aria-hidden="true">2.5.</strong> 联合类型和交叉类型</a></li><li class="chapter-item expanded "><a href="../../zh/handbook/classes.html"><strong aria-hidden="true">2.6.</strong> 类</a></li><li class="chapter-item expanded "><a href="../../zh/handbook/enums.html"><strong aria-hidden="true">2.7.</strong> 枚举</a></li><li class="chapter-item expanded "><a href="../../zh/handbook/generics.html"><strong aria-hidden="true">2.8.</strong> 泛型</a></li></ol></li><li class="chapter-item expanded "><li class="part-title">手册（进阶）</li><li class="chapter-item expanded "><a href="../../zh/reference/index.html"><strong aria-hidden="true">3.</strong> 手册（进阶）</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../zh/reference/advanced-types.html"><strong aria-hidden="true">3.1.</strong> 高级类型</a></li><li class="chapter-item expanded "><a href="../../zh/reference/utility-types.html"><strong aria-hidden="true">3.2.</strong> 实用工具类型</a></li><li class="chapter-item expanded "><a href="../../zh/reference/decorators.html"><strong aria-hidden="true">3.3.</strong> Decorators</a></li><li class="chapter-item expanded "><a href="../../zh/reference/declaration-merging.html"><strong aria-hidden="true">3.4.</strong> 声明合并</a></li><li class="chapter-item expanded "><a href="../../zh/reference/iterators-and-generators.html"><strong aria-hidden="true">3.5.</strong> Iterators 和 Generators</a></li><li class="chapter-item expanded "><a href="../../zh/reference/jsx.html"><strong aria-hidden="true">3.6.</strong> JSX</a></li><li class="chapter-item expanded "><a href="../../zh/reference/mixins.html"><strong aria-hidden="true">3.7.</strong> 混入</a></li><li class="chapter-item expanded "><a href="../../zh/reference/modules.html"><strong aria-hidden="true">3.8.</strong> 模块</a></li><li class="chapter-item expanded "><a href="../../zh/reference/module-resolution.html"><strong aria-hidden="true">3.9.</strong> 模块解析</a></li><li class="chapter-item expanded "><a href="../../zh/reference/namespaces.html"><strong aria-hidden="true">3.10.</strong> 命名空间</a></li><li class="chapter-item expanded "><a href="../../zh/reference/namespaces-and-modules.html"><strong aria-hidden="true">3.11.</strong> 命名空间和模块</a></li><li class="chapter-item expanded "><a href="../../zh/reference/symbols.html"><strong aria-hidden="true">3.12.</strong> Symbols</a></li><li class="chapter-item expanded "><a href="../../zh/reference/triple-slash-directives.html"><strong aria-hidden="true">3.13.</strong> 三斜线指令</a></li><li class="chapter-item expanded "><a href="../../zh/reference/type-compatibility.html"><strong aria-hidden="true">3.14.</strong> 类型兼容性</a></li><li class="chapter-item expanded "><a href="../../zh/reference/type-inference.html"><strong aria-hidden="true">3.15.</strong> 类型推论</a></li><li class="chapter-item expanded "><a href="../../zh/reference/variable-declarations.html"><strong aria-hidden="true">3.16.</strong> 变量声明</a></li></ol></li><li class="chapter-item expanded "><li class="part-title">手册（v2）</li><li class="chapter-item expanded "><a href="../../zh/handbook-v2/index.html"><strong aria-hidden="true">4.</strong> 手册（v2）</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../zh/handbook-v2/type-manipulation/template-literal-types.html"><strong aria-hidden="true">4.1.</strong> 模版字面量类型</a></li></ol></li><li class="chapter-item expanded "><li class="part-title">TypeScript 声明文件（.d.ts）</li><li class="chapter-item expanded "><a href="../../zh/declaration-files/index.html"><strong aria-hidden="true">5.</strong> 如何书写声明文件</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../zh/declaration-files/introduction.html"><strong aria-hidden="true">5.1.</strong> 介绍</a></li><li class="chapter-item expanded "><a href="../../zh/declaration-files/by-example.html"><strong aria-hidden="true">5.2.</strong> 举例</a></li><li class="chapter-item expanded "><a href="../../zh/declaration-files/library-structures.html"><strong aria-hidden="true">5.3.</strong> 库结构</a></li><li class="chapter-item expanded "><a href="../../zh/declaration-files/templates.html"><strong aria-hidden="true">5.4.</strong> 模板</a></li><li class="chapter-item expanded "><a href="../../zh/declaration-files/do-s-and-don-ts.html"><strong aria-hidden="true">5.5.</strong> 最佳实践</a></li><li class="chapter-item expanded "><a href="../../zh/declaration-files/deep-dive.html"><strong aria-hidden="true">5.6.</strong> 深入</a></li><li class="chapter-item expanded "><a href="../../zh/declaration-files/publishing.html"><strong aria-hidden="true">5.7.</strong> 发布</a></li><li class="chapter-item expanded "><a href="../../zh/declaration-files/consumption.html"><strong aria-hidden="true">5.8.</strong> 使用</a></li></ol></li><li class="chapter-item expanded "><li class="part-title">TypeScript for JavaScript</li><li class="chapter-item expanded "><a href="../../zh/javascript/type-checking-javascript-files.html"><strong aria-hidden="true">6.</strong> JavaScript 文件里的类型检查</a></li><li class="chapter-item expanded affix "><li class="part-title">工程配置</li><li class="chapter-item expanded "><a href="../../zh/project-config/index.html"><strong aria-hidden="true">7.</strong> 工程配置</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../zh/project-config/tsconfig.json.html"><strong aria-hidden="true">7.1.</strong> tsconfig.json</a></li><li class="chapter-item expanded "><a href="../../zh/project-config/project-references.html"><strong aria-hidden="true">7.2.</strong> 工程引用</a></li><li class="chapter-item expanded "><a href="../../zh/project-config/typings-for-npm-packages.html"><strong aria-hidden="true">7.3.</strong> NPM 包的类型</a></li><li class="chapter-item expanded "><a href="../../zh/project-config/compiler-options.html"><strong aria-hidden="true">7.4.</strong> 编译选项</a></li><li class="chapter-item expanded "><a href="../../zh/project-config/configuring-watch.html"><strong aria-hidden="true">7.5.</strong> 配置 Watch</a></li><li class="chapter-item expanded "><a href="../../zh/project-config/compiler-options-in-msbuild.html"><strong aria-hidden="true">7.6.</strong> 在 MSBuild 里使用编译选项</a></li><li class="chapter-item expanded "><a href="../../zh/project-config/integrating-with-build-tools.html"><strong aria-hidden="true">7.7.</strong> 与其它构建工具整合</a></li><li class="chapter-item expanded "><a href="../../zh/project-config/nightly-builds.html"><strong aria-hidden="true">7.8.</strong> 使用 TypeScript 的每日构建版本</a></li></ol></li><li class="chapter-item expanded "><li class="part-title">版本发布说明（Release Notes）</li><li class="chapter-item expanded "><a href="../../zh/release-notes/index.html"><strong aria-hidden="true">8.</strong> 新增功能</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-5.1.html"><strong aria-hidden="true">8.1.</strong> TypeScript 5.1</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-5.0.html"><strong aria-hidden="true">8.2.</strong> TypeScript 5.0</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.9.html"><strong aria-hidden="true">8.3.</strong> TypeScript 4.9</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.8.html"><strong aria-hidden="true">8.4.</strong> TypeScript 4.8</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.7.html"><strong aria-hidden="true">8.5.</strong> TypeScript 4.7</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.6.html" class="active"><strong aria-hidden="true">8.6.</strong> TypeScript 4.6</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.5.html"><strong aria-hidden="true">8.7.</strong> TypeScript 4.5</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.4.html"><strong aria-hidden="true">8.8.</strong> TypeScript 4.4</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.3.html"><strong aria-hidden="true">8.9.</strong> TypeScript 4.3</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.2.html"><strong aria-hidden="true">8.10.</strong> TypeScript 4.2</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.1.html"><strong aria-hidden="true">8.11.</strong> TypeScript 4.1</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-4.0.html"><strong aria-hidden="true">8.12.</strong> TypeScript 4.0</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.9.html"><strong aria-hidden="true">8.13.</strong> TypeScript 3.9</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.8.html"><strong aria-hidden="true">8.14.</strong> TypeScript 3.8</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.7.html"><strong aria-hidden="true">8.15.</strong> TypeScript 3.7</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.6.html"><strong aria-hidden="true">8.16.</strong> TypeScript 3.6</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.5.html"><strong aria-hidden="true">8.17.</strong> TypeScript 3.5</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.4.html"><strong aria-hidden="true">8.18.</strong> TypeScript 3.4</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.3.html"><strong aria-hidden="true">8.19.</strong> TypeScript 3.3</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.2.html"><strong aria-hidden="true">8.20.</strong> TypeScript 3.2</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.1.html"><strong aria-hidden="true">8.21.</strong> TypeScript 3.1</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-3.0.html"><strong aria-hidden="true">8.22.</strong> TypeScript 3.0</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.9.html"><strong aria-hidden="true">8.23.</strong> TypeScript 2.9</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.8.html"><strong aria-hidden="true">8.24.</strong> TypeScript 2.8</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.7.html"><strong aria-hidden="true">8.25.</strong> TypeScript 2.7</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.6.html"><strong aria-hidden="true">8.26.</strong> TypeScript 2.6</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.5.html"><strong aria-hidden="true">8.27.</strong> TypeScript 2.5</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.4.html"><strong aria-hidden="true">8.28.</strong> TypeScript 2.4</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.3.html"><strong aria-hidden="true">8.29.</strong> TypeScript 2.3</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.2.html"><strong aria-hidden="true">8.30.</strong> TypeScript 2.2</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.1.html"><strong aria-hidden="true">8.31.</strong> TypeScript 2.1</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-2.0.html"><strong aria-hidden="true">8.32.</strong> TypeScript 2.0</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-1.8.html"><strong aria-hidden="true">8.33.</strong> TypeScript 1.8</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-1.7.html"><strong aria-hidden="true">8.34.</strong> TypeScript 1.7</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-1.6.html"><strong aria-hidden="true">8.35.</strong> TypeScript 1.6</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-1.5.html"><strong aria-hidden="true">8.36.</strong> TypeScript 1.5</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-1.4.html"><strong aria-hidden="true">8.37.</strong> TypeScript 1.4</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-1.3.html"><strong aria-hidden="true">8.38.</strong> TypeScript 1.3</a></li><li class="chapter-item expanded "><a href="../../zh/release-notes/typescript-1.1.html"><strong aria-hidden="true">8.39.</strong> TypeScript 1.1</a></li></ol></li><li class="chapter-item expanded "><li class="part-title">破坏性改动（Breaking Changes）</li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/index.html"><strong aria-hidden="true">9.</strong> Breaking Changes</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-3.6.html"><strong aria-hidden="true">9.1.</strong> TypeScript 3.6</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-3.5.html"><strong aria-hidden="true">9.2.</strong> TypeScript 3.5</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-3.4.html"><strong aria-hidden="true">9.3.</strong> TypeScript 3.4</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-3.2.html"><strong aria-hidden="true">9.4.</strong> TypeScript 3.2</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-3.1.html"><strong aria-hidden="true">9.5.</strong> TypeScript 3.1</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-3.0.html"><strong aria-hidden="true">9.6.</strong> TypeScript 3.0</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-2.9.html"><strong aria-hidden="true">9.7.</strong> TypeScript 2.9</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-2.8.html"><strong aria-hidden="true">9.8.</strong> TypeScript 2.8</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-2.7.html"><strong aria-hidden="true">9.9.</strong> TypeScript 2.7</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-2.6.html"><strong aria-hidden="true">9.10.</strong> TypeScript 2.6</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-2.4.html"><strong aria-hidden="true">9.11.</strong> TypeScript 2.4</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-2.3.html"><strong aria-hidden="true">9.12.</strong> TypeScript 2.3</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-2.2.html"><strong aria-hidden="true">9.13.</strong> TypeScript 2.2</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-2.1.html"><strong aria-hidden="true">9.14.</strong> TypeScript 2.1</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-2.0.html"><strong aria-hidden="true">9.15.</strong> TypeScript 2.0</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-1.8.html"><strong aria-hidden="true">9.16.</strong> TypeScript 1.8</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-1.7.html"><strong aria-hidden="true">9.17.</strong> TypeScript 1.7</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-1.6.html"><strong aria-hidden="true">9.18.</strong> TypeScript 1.6</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-1.5.html"><strong aria-hidden="true">9.19.</strong> TypeScript 1.5</a></li><li class="chapter-item expanded "><a href="../../zh/breaking-changes/typescript-1.4.html"><strong aria-hidden="true">9.20.</strong> TypeScript 1.4</a></li></ol></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">TypeScript 使用指南手册</h1>

                    <div class="right-buttons">
                        <a href="../../print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>

                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script>
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <h1 id="typescript-46"><a class="header" href="#typescript-46">TypeScript 4.6</a></h1>
<h3 id="允许在构造函数中的-super-调用之前插入代码"><a class="header" href="#允许在构造函数中的-super-调用之前插入代码">允许在构造函数中的 <code>super()</code> 调用之前插入代码</a></h3>
<p>在 JavaScript 的类中，在引用 <code>this</code> 之前必须先调用 <code>super()</code>。
在 TypeScript 中同样有这个限制，只不过在检查时过于严格。
在之前版本的 TypeScript 中，如果类中存在<strong>属性初始化器</strong>，
那么在构造函数里，在 <code>super()</code> 调用之前不允许出现任何其它代码。</p>
<pre><code class="language-ts">class Base {
    // ...
}

class Derived extends Base {
    someProperty = true;

    constructor() {
        // 错误！
        // 必须先调用 'super()' 因为需要初始化 'someProperty'。
        doSomeStuff();
        super();
    }
}
</code></pre>
<p>这样做是因为程序实现起来容易，但这样做也会拒绝很多合法的代码。
TypeScript 4.6 放宽了限制，它允许在 <code>super()</code> 之前出现其它代码，
与此同时仍然会检查在引用 <code>this</code> 之前顶层的<code>super()</code> 已经被调用。</p>
<p>感谢 <a href="https://github.com/JoshuaKGoldberg">Joshua Goldberg</a> 的 <a href="https://github.com/microsoft/TypeScript/pull/29374">PR</a>。</p>
<h3 id="基于控制流来分析解构的可辨识联合类型"><a class="header" href="#基于控制流来分析解构的可辨识联合类型">基于控制流来分析解构的可辨识联合类型</a></h3>
<p>TypeScript 可以根据判别式属性来细化类型。
例如，在下面的代码中，TypeScript 能够在检查 <code>kind</code> 的类型后细化 <code>action</code> 的类型。</p>
<pre><code class="language-ts">type Action =
    | { kind: &quot;NumberContents&quot;, payload: number }
    | { kind: &quot;StringContents&quot;, payload: string };

function processAction(action: Action) {
    if (action.kind === &quot;NumberContents&quot;) {
        // `action.payload` is a number here.
        let num = action.payload * 2
        // ...
    }
    else if (action.kind === &quot;StringContents&quot;) {
        // `action.payload` is a string here.
        const str = action.payload.trim();
        // ...
    }
}
</code></pre>
<p>这样就可以使用持有不同数据的对象，但通过共同的字段来区分它们。</p>
<p>这在 TypeScript 是很常见的；然而，根据个人的喜好，你可能想对上例中的 <code>kind</code> 和 <code>payload</code> 进行解构。
就像下面这样：</p>
<pre><code class="language-ts">type Action =
    | { kind: &quot;NumberContents&quot;, payload: number }
    | { kind: &quot;StringContents&quot;, payload: string };

function processAction(action: Action) {
    const { kind, payload } = action;
    if (kind === &quot;NumberContents&quot;) {
        let num = payload * 2
        // ...
    }
    else if (kind === &quot;StringContents&quot;) {
        const str = payload.trim();
        // ...
    }
}
</code></pre>
<p>此前，TypeScript 会报错 - 当 <code>kind</code> 和 <code>payload</code> 是由同一个对象解构为变量时，它们会被独立对待。</p>
<p>在 TypeScript 4.6 中可以正常工作！</p>
<p>当解构独立的属性为 const 声明，或当解构参数到变量且没有重新赋值时，TypeScript 会检查被解构的类型是否为可辨识联合。
如果是的话，TypeScript 就能够根据类型检查来细化变量的类型。
因此上例中，通过检查 <code>kind</code> 的类型可以细化 <code>payload</code> 的类型。</p>
<p>更多详情请查看 <a href="https://github.com/microsoft/TypeScript/pull/46266">PR</a>。</p>
<h3 id="改进的递归深度检查"><a class="header" href="#改进的递归深度检查">改进的递归深度检查</a></h3>
<p>TypeScript 要面对一些有趣的挑战，因为它是构建在结构化类型系统之上，同时又支持了泛型。</p>
<p>在结构化类型系统中，对象类型的兼容性是由对象包含的成员决定的。</p>
<pre><code class="language-ts">interface Source {
    prop: string;
}

interface Target {
    prop: number;
}

function check(source: Source, target: Target) {
    target = source;
    // error!
    // Type 'Source' is not assignable to type 'Target'.
    //   Types of property 'prop' are incompatible.
    //     Type 'string' is not assignable to type 'number'.
}
</code></pre>
<p><code>Source</code> 与 <code>Target</code> 的兼容性取决于它们的<em>属性</em>是否可以执行赋值操作。
此例中是指 <code>prop</code> 属性。</p>
<p>当引入了泛型后，有一些难题需要解决。
例如，下例中的 <code>Source&lt;string&gt;</code> 是否可以赋值给 <code>Target&lt;number&gt;</code>？</p>
<pre><code class="language-ts">interface Source&lt;T&gt; {
    prop: Source&lt;Source&lt;T&gt;&gt;;
}

interface Target&lt;T&gt; {
    prop: Target&lt;Target&lt;T&gt;&gt;;
}

function check(source: Source&lt;string&gt;, target: Target&lt;number&gt;) {
    target = source;
}
</code></pre>
<p>要想回答这个问题，TypeScript 需要检查 <code>prop</code> 的类型是否兼容。
这又要回答另一个问题：<code>Source&lt;Source&lt;string&gt;&gt;</code> 是否能够赋值给 <code>Target&lt;Target&lt;number&gt;&gt;</code>？
要想回答这个问题，TypeScript 需要检查 <code>prop</code> 的类型是否与那些类型兼容，
结果就是还要检查 <code>Source&lt;Source&lt;Source&lt;string&gt;&gt;&gt;</code> 是否能够赋值给 <code>Target&lt;Target&lt;Target&lt;number&gt;&gt;&gt;</code>？
继续发展下去，就会注意到类型会进行无限展开。</p>
<p>TypeScript 使用了启发式的算法 - 当一个类型达到特定的检查深度时，它表现出了将会进行无限展开，
那么就认为它<em>可能</em>是兼容的。
通常情况下这是没问题的，但是也可能出现漏报的情况。</p>
<pre><code class="language-ts">interface Foo&lt;T&gt; {
    prop: T;
}

declare let x: Foo&lt;Foo&lt;Foo&lt;Foo&lt;Foo&lt;Foo&lt;string&gt;&gt;&gt;&gt;&gt;&gt;;
declare let y: Foo&lt;Foo&lt;Foo&lt;Foo&lt;Foo&lt;string&gt;&gt;&gt;&gt;&gt;;

x = y;
</code></pre>
<p>通过人眼观察我们知道上例中的 <code>x</code> 和 <code>y</code> 是不兼容的。
虽然类型的嵌套层次很深，但人家就是这样声明的。
启发式算法要处理的是在探测类型过程中生成的深层次嵌套类型，而非程序员明确手写出的类型。</p>
<p>TypeScript 4.6 现在能够区分出这类情况，并且对上例进行正确的错误提示。
此外，由于不再担心会对明确书写的类型进行误报，
TypeScript 能够更容易地判断类型的无限展开，
并且降低了类型兼容性检查的成本。
因此，像 DefinitelyTyped 上的 <code>redux-immutable</code> 、 <code>react-lazylog</code> 和 <code>yup</code>
代码库，对它们的类型检查时间降低了 50%。</p>
<p>你可能已经体验过这个改动了，因为它被挑选合并到了 TypeScript 4.5.3 中，
但它仍然是 TypeScript 4.6 中值得关注的一个特性。
更多详情请阅读 <a href="https://github.com/microsoft/TypeScript/pull/46599">PR</a>。</p>
<h3 id="索引访问类型推断改进"><a class="header" href="#索引访问类型推断改进">索引访问类型推断改进</a></h3>
<p>TypeScript 现在能够正确地推断通过索引访问到另一个映射对象类型的类型。</p>
<pre><code class="language-ts">interface TypeMap {
    &quot;number&quot;: number;
    &quot;string&quot;: string;
    &quot;boolean&quot;: boolean;
}

type UnionRecord&lt;P extends keyof TypeMap&gt; = { [K in P]:
    {
        kind: K;
        v: TypeMap[K];
        f: (p: TypeMap[K]) =&gt; void;
    }
}[P];

function processRecord&lt;K extends keyof TypeMap&gt;(record: UnionRecord&lt;K&gt;) {
    record.f(record.v);
}

// 这个调用之前是有问题的，但现在没有问题
processRecord({
    kind: &quot;string&quot;,
    v: &quot;hello!&quot;,

    // 'val' 之前会隐式地获得类型 'string | number | boolean'，
    // 但现在会正确地推断为类型 'string'。
    f: val =&gt; {
        console.log(val.toUpperCase());
    }
})
</code></pre>
<p>该模式已经被支持了并允许 TypeScript 判断 <code>record.f(record.v)</code> 调用是合理的，
但是在以前，<code>processRecord</code> 调用中对 <code>val</code> 的类型推断并不好。</p>
<p>TypeScript 4.6 改进了这个情况，因此在启用 <code>processRecord</code> 时不再需要使用类型断言。</p>
<p>更多详情请阅读 <a href="https://github.com/microsoft/TypeScript/pull/47109">PR</a>。</p>
<h3 id="对因变参数的控制流分析"><a class="header" href="#对因变参数的控制流分析">对因变参数的控制流分析</a></h3>
<p>函数签名可以声明为剩余参数且其类型可以为可辨识联合元组类型。</p>
<pre><code class="language-ts">function func(...args: [&quot;str&quot;, string] | [&quot;num&quot;, number]) {
    // ...
}
</code></pre>
<p>这意味着 <code>func</code> 的实际参数完全依赖于第一个实际参数。
若第一个参数为字符串 <code>&quot;str&quot;</code> 时，则第二个参数为 <code>string</code> 类型。
若第一个参数为字符串 <code>&quot;num&quot;</code> 时，则第二个参数为 <code>number</code> 类型。</p>
<p>像这样 TypeScript 是由签名来推断函数类型时，TypeScript 能够根据依赖的参数来细化类型。</p>
<pre><code class="language-ts">type Func = (...args: [&quot;a&quot;, number] | [&quot;b&quot;, string]) =&gt; void;

const f1: Func = (kind, payload) =&gt; {
    if (kind === &quot;a&quot;) {
        payload.toFixed();  // 'payload' narrowed to 'number'
    }
    if (kind === &quot;b&quot;) {
        payload.toUpperCase();  // 'payload' narrowed to 'string'
    }
};

f1(&quot;a&quot;, 42);
f1(&quot;b&quot;, &quot;hello&quot;);
</code></pre>
<p>更多详情请阅读 <a href="https://github.com/microsoft/TypeScript/pull/47190">PR</a>。</p>
<h3 id="--target-es2022"><a class="header" href="#--target-es2022">--target es2022</a></h3>
<p>TypeScript 的 <code>--target</code> 编译选项现在支持使用 <code>es2022</code>。
这意味着像类字段这样的特性能够稳定地在输出结果中保留。
这也意味着像 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn">Arrays 的上 at() 和 Object.hasOwn 方法</a>
或者 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#rethrowing_an_error_with_a_cause">new Error 时的 <code>cause</code> 选项</a>
可以通过设置新的 <code>--target</code> 或者 <code>--lib es2022</code> 来使用。</p>
<p>感谢 <a href="https://github.com/saschanaz">Kagami Sascha Rosylight (saschanaz)</a> 的<a href="https://github.com/microsoft/TypeScript/pull/46291">实现</a>。</p>
<h3 id="删除-react-jsx-中不必要的参数"><a class="header" href="#删除-react-jsx-中不必要的参数">删除 react-jsx 中不必要的参数</a></h3>
<p>在以前，当使用 <code>--jsx react-jsx</code> 来编译如下的代码时</p>
<pre><code class="language-ts">export const el = &lt;div&gt;foo&lt;/div&gt;;
</code></pre>
<p>TypeScript 会生成如下的 JavaScript 代码：</p>
<pre><code class="language-ts">import { jsx as _jsx } from &quot;react/jsx-runtime&quot;;
export const el = _jsx(&quot;div&quot;, { children: &quot;foo&quot; }, void 0);
</code></pre>
<p>末尾的 <code>void 0</code> 参数是没用的，删掉它会减小打包的体积。</p>
<p>感谢 <a href="https://github.com/a-tarasyuk">https://github.com/a-tarasyuk</a> 的 <a href="https://github.com/microsoft/TypeScript/pull/47467">PR</a>，TypeScript 4.6 会删除 <code>void 0</code> 参数。</p>
<h3 id="jsdoc-命名建议"><a class="header" href="#jsdoc-命名建议">JSDoc 命名建议</a></h3>
<p>在 JSDoc 里，你可以用 <code>@param</code> 标签来文档化参数。</p>
<pre><code class="language-js">/**
 * @param x The first operand
 * @param y The second operand
 */
function add(x, y) {
    return x + y;
}
</code></pre>
<p>但是，如果这些注释已经过时了会发生什么？就比如，我们将 <code>x</code> 和 <code>y</code> 重命名为 <code>a</code> 和 <code>b</code>？</p>
<pre><code class="language-js">/**
 * @param x {number} The first operand
 * @param y {number} The second operand
 */
function add(a, b) {
    return a + b;
}
</code></pre>
<p>在之前 TypeScript 仅会在对 JavaScript 文件执行类型检查时报告这个问题 - 通过
使用 <code>checkJs</code> 选项，或者在文件顶端添加 <code>// @ts-check</code> 注释。</p>
<p>现在，你能够在编译器中的 TypeScript 文件上看到类似的提示！
TypeScript 现在会给出建议，如果函数签名中的参数名与 JSDoc 中的参数名不一致。</p>
<p><img src="https://devblogs.microsoft.com/typescript/wp-content/uploads/sites/11/2022/02/jsdoc-comment-suggestions-4-6.png" alt="example" /></p>
<p>该<a href="https://github.com/microsoft/TypeScript/pull/47257">改动</a>是由 <a href="https://github.com/a-tarasyuk">Alexander Tarasyuk</a> 提供的！</p>
<h3 id="javascript-中更多的语法和绑定错误提示"><a class="header" href="#javascript-中更多的语法和绑定错误提示">JavaScript 中更多的语法和绑定错误提示</a></h3>
<p>TypeScript 将更多的语法和绑定错误检查应用到了 JavaScript 文件上。
如果你在 Visual Studio 或 Visual Studio Code 这样的编辑器中打开 JavaScript 文件时就会看到这些新的错误提示，
或者当你使用 TypeScript 编译器来处理 JavaScript 文件时 - 即便你没有打开 <code>checkJs</code> 或者添加 <code>// @ts-check</code> 注释。</p>
<p>做为例子，如果在 JavaScript 文件中的同一个作用域中有两个同名的 <code>const</code> 声明，
那么 TypeScript 会报告一个错误。</p>
<pre><code class="language-js">const foo = 1234;
//    ~~~
// error: Cannot redeclare block-scoped variable 'foo'.

// ...

const foo = 5678;
//    ~~~
// error: Cannot redeclare block-scoped variable 'foo'.
</code></pre>
<p>另外一个例子，TypeScript 会报告修饰符是否被正确地使用了。</p>
<pre><code class="language-js">function container() {
    export function foo() {
//  ~~~~~~
// error: Modifiers cannot appear here.

    }
}
</code></pre>
<p>这些检查可以通过在文件顶端添加 <code>// @ts-nocheck</code> 注释来禁用，
但是我们很想听听在大家的 JavaScript 工作流中使用该特性的反馈。
你可以在 Visual Studio Code 安装 <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-typescript-next">TypeScript 和 JavaScript Nightly 扩展</a> 来提前体验，
并阅读 <a href="https://github.com/microsoft/TypeScript/pull/47067">PR1</a> 和 <a href="https://github.com/microsoft/TypeScript/pull/47075">PR1</a>。</p>
<h3 id="typescript-trace-分析器"><a class="header" href="#typescript-trace-分析器">TypeScript Trace 分析器</a></h3>
<p>有人偶尔会遇到创建和比较类型时很耗时的情况。
TypeScript 提供了一个 <a href="https://github.com/microsoft/TypeScript/wiki/Performance#performance-tracing"><code>--generateTrace</code></a> 选项来帮助识别耗时的类型，
或者帮助诊断 TypeScript 编译器中的问题。
虽说由 <code>--generateTrace</code> 生成的信息是非常有帮助的（尤其是在 TypeScript 4.6 的改进后），
但是阅读这些 trace 信息是比较难的。</p>
<p>近期，我们发布了 <a href="https://www.npmjs.com/package/@typescript/analyze-trace">@typescript/analyze-trace</a> 工具来帮助阅读这些信息。
虽说我们不认为每个人都需要使用 <code>analyze-trace</code>，但是我们认为它会为遇到了 <a href="https://github.com/microsoft/TypeScript/wiki/Performance">TypeScript 构建性能</a>问题的团队提供帮助。</p>
<p>更多详情请查看 <a href="https://github.com/microsoft/typescript-analyze-trace">repo</a>。</p>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                            <a rel="prev" href="../../zh/release-notes/typescript-4.7.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>

                            <a rel="next" href="../../zh/release-notes/typescript-4.5.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                    <a rel="prev" href="../../zh/release-notes/typescript-4.7.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>

                    <a rel="next" href="../../zh/release-notes/typescript-4.5.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>




        <script>
            window.playground_copyable = true;
        </script>


        <script src="../../elasticlunr.min.js"></script>
        <script src="../../mark.min.js"></script>
        <script src="../../searcher.js"></script>

        <script src="../../clipboard.min.js"></script>
        <script src="../../highlight.js"></script>
        <script src="../../book.js"></script>

        <!-- Custom JS scripts -->


    </div>
    </body>
</html>
